import Vue from 'vue'
import Router from 'vue-router'
// import store from './store/index'


Vue.use(Router);

const router = new Router({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes: [{
            path: '/',
            name: 'home',
            redirect: '/entrance/111',
            component: () =>
                import ('@/views/Home.vue'),
            children: [{
                    path: '/entrance/111',
                    name: 'entrance',
                    component: () =>
                        import ('@/views/Entrance.vue'),
                },
                {
                    path: '/entrance/113',
                    name: 'entrance',
                    component: () =>
                        import ('@/views/Entrance.vue')
                },
                {
                    path: "/user/settings",
                    name: "settings",
                    meta: {
                        hasLogin: true
                    },
                    component: () =>
                        import ('@/components/UserSettings.vue')
                },
                {
                    path: '/user/:uid',
                    name: 'user',
                    redirect: '/user/:uid/posts',
                    component: () =>
                        import ('@/components/User.vue'),

                    children: [{
                            path: '/user/:uid/posts',
                            name: 'posts',
                            meta: {
                                root: 'posts'
                            },
                            component: () =>
                                import ('@/components/UserPosts.vue')
                        },
                        {
                            path: '/user/:uid/collections',
                            name: 'collections',
                            meta: {
                                root: 'collections'
                            },
                            component: () =>
                                import ('@/components/UserCollections.vue')
                        },
                        {
                            path: '/user/:uid/follows',
                            name: 'follows',
                            redirect: '/user/:uid/followings',
                            component: () =>
                                import ('@/components/UserFollows.vue'),
                            children: [{
                                    path: '/user/:uid/followers',
                                    name: 'followers',
                                    meta: {
                                        root: 'follows'
                                    },
                                    component: () =>
                                        import ('@/components/Followers.vue')
                                },
                                {
                                    path: '/user/:uid/followings',
                                    name: 'followings',
                                    meta: {
                                        root: 'follows'
                                    },
                                    component: () =>
                                        import ('@/components/Followings.vue')
                                },
                            ]
                        },

                    ]
                },

                {
                    path: '/post/:aid',
                    name: 'user',
                    component: () =>
                        import ('@/components/SingleArticle.vue')
                },
                {
                    path: '/collection/:cid',
                    name: 'colloction',
                    component: () =>
                        import ('@/components/Collections.vue')
                },
                {
                    path: "/search",
                    name: 'search',
                    component: () =>
                        import ('@/components/Search.vue')
                }

            ]
        },
        {
            path: '/editor',
            name: 'editor',
            meta: {
                hasLogin: true
            },
            component: () =>
                import ('@/components/Editor.vue')
        },
        {
            path: '*',
            component: () =>
                import ('@/views/Home.vue'),
            children: [{
                path: "*",
                component: () =>
                    import ('@/views/NotFound.vue'),
            }]
        },


    ],
    //解决滚动条被记住的问题
    scrollBehavior(to, from, savedPssition) {
        if (savedPssition) {
            return savedPssition;
        } else {
            return { x: 0, y: 0 }
        }
    }
});
router.beforeEach((to, from, next) => {
    if (to.meta.hasLogin) {
        const isAuthenticated = localStorage.auth
        if (isAuthenticated) {
            next()
        } else {
            next({
                path: "/"
            })
        }


    } else {
        next();
    }
})


const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
};

export default router;